package com.wmzdq.photocenter.controller;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wmzdq.photocenter.bean.FileBean;
import com.wmzdq.photocenter.bean.MenuBean;
import com.wmzdq.photocenter.common.CoverMenu;
import com.wmzdq.photocenter.common.PropertyUtil;
import com.wmzdq.photocenter.file.IFileOperator;

@RestController
@RequestMapping(value="/directory")
public class DirectoryController {

	@Autowired
	IFileOperator operator;
	
	@Autowired
	CoverMenu coverUtil;
	
	@Autowired
	PropertyUtil proUtil;
	
	@RequestMapping(value="/getDirectories" ,method=RequestMethod.GET)
	public String getDirectories(@RequestParam(defaultValue="",required=false) String path){
		String[] directories=operator.getDirectories(path);
		List<MenuBean> list=new ArrayList<MenuBean>();
		for (String d : directories) {
			list.add(coverUtil.cover(d));
		}
		
		return JSON.toJSONString(list);
	}
	
	@RequestMapping(value="/getFiles" ,method=RequestMethod.GET)
	public String getFiles(@RequestParam(defaultValue="",required=false) String path,@RequestParam int page,@RequestParam int size) throws InterruptedException{
		String[] filenames;
		String[] copy;
		int index;
		try {
			filenames = operator.getFileNames(URLDecoder.decode(path,"UTF-8"));
			if(filenames.length==0){
				return "{}";
			}
			//开始的序列
			index=(page-1)*size;
			//如果开始的序列 大于 文件总长度 那么说明到头了
			if(index>filenames.length){
				return "{}";
			}
			//剩余的长度 小于 size，调整size大小
			if((filenames.length-index)<size){
				size=filenames.length-index;
			}
			copy=new String[size];
			System.arraycopy(filenames, index, copy, 0, size);
			List<FileBean> list=new ArrayList<FileBean>();
			for (String d : copy) {
				//先判断是否有mov格式
				list.add(coverUtil.coverFile(d,index,path+File.separator+operator.getThum()+File.separator+d,path,operator.getVideo(path, d)));
				index++;
			}
			
			return JSON.toJSONString(list);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	@RequestMapping(value="/photo",method=RequestMethod.GET)
	public ModelAndView getUser(){
		return new ModelAndView("photo");  
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public @ResponseBody JSONObject insert(@RequestParam String name,HttpServletRequest request) throws UnsupportedEncodingException{
		request.setCharacterEncoding("UTF-8");

		String xx = new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8");
		System.out.println(xx);
		JSONObject result=new JSONObject();
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
		String direname=sdf.format(new Date());
		if(proUtil.set(xx, direname)){
			//建立文件夹
			operator.create(direname);
			result.put("flag", true);
			result.put("url", direname);
			return result;
		}
		result.put("flag", false);
		return result;
	}
}
